﻿

using RestSharp;

namespace eslib.NetUpdator
{
    /// <summary>
    /// 网络下载器
    /// </summary>
    public static class NetDownloader
    {
        /// <summary>
        /// 下载网络数据以String形式返回
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async Task<(bool success, string result)> GetNetText(string url)
        {
            RestClient client = new RestClient(url);
            RestRequest request = new();
            RestResponse response = await client.GetAsync(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return (true, response.Content);
            }
            else
            {
                return (false, null);
            }
        }


        /// <summary>
        /// 下载网络数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async Task<(bool success, byte[] result)> Download(string url)
        {
            RestClient client = new RestClient(url);
            RestRequest request = new();

            RestResponse response = await client.GetAsync(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return (true, response.RawBytes);
            }
            else
            {
                return (false, null);
            }
        }


    }



}
